package xcmg.device.web.ratingManager;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.ratingManager.RatingInfoService;
import xcmg.device.service.vo.ratingManager.RatingInfoQueryVO;
import xcmg.device.service.vo.ratingManager.RatingInfoVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import java.util.Arrays;

/**
 * Created by wrg on 2019/7/10.
 */
@RestController
@RequestMapping("/ratingInfo")
@Api("评级管理信息入口")
public class RatingInfoController extends BasicController {
    @Autowired
    private RatingInfoService ratingInfoService;

    @PostMapping(value = "/add")
    @ApiOperation(value = "新增评级管理主信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult add(@RequestBody RatingInfoVO ratingInfoVO) throws Exception {
        if (null == ratingInfoVO){
            return actionResult(ErrorCode.IllegalArument);
        }

        ratingInfoVO.setCreatorId(getUserId());
        ratingInfoVO.setCreatorName(getUserName());
        ratingInfoVO.setOrgId(getOrgId());
        return actionResult(ratingInfoService.add(ratingInfoVO));
    }

    @PostMapping(value = "/delete")
    @ApiOperation(value = "删除评级信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult remove(@RequestBody String[] ids) throws Exception {
        if (ids == null || ids.length == 0) {
            return actionResult(ErrorCode.IllegalArument);
        }
        try{
            ErrorCode ret = ratingInfoService.remove(Arrays.asList(ids));
            return actionResult(ret);
        }catch (Exception e){
            return new ActionResult<>(ErrorCode.Failure.getCode(),e.getMessage(),null);
        }
    }

    @PostMapping(value = "/update")
    @ApiOperation(value = "修改评级管理主信息")
    @FastMappingInfo(needLogin = true)
    public ActionResult update(@RequestBody RatingInfoVO ratingInfoVO) throws Exception {
        if (null == ratingInfoVO){
            return actionResult(ErrorCode.IllegalArument);
        }

        return actionResult(ratingInfoService.update(ratingInfoVO,getUserId(),getUserName()));
    }

    @PostMapping(value = "/list")
    @ApiOperation(value = "获取评级列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<RatingInfoQueryVO> condition) throws Exception {
        if (condition.getFilters() == null) {
            RatingInfoQueryVO filters = new RatingInfoQueryVO();
            filters.setOrgId(getOrgId());
            condition.setFilters(filters);
        } else {
            condition.getFilters().setOrgId(getOrgId());
        }
        PageCommonVO pageCommonVO = ratingInfoService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @GetMapping(value = "/item")
    @ApiOperation(value = "获取评级详情")
    @FastMappingInfo(needLogin = true)
    public ActionResult item(@RequestParam String id) throws Exception {
        if (StringUtils.isBlank(id)) {
           return actionResult(ErrorCode.IllegalArument);
        }
        return actionValueResult(ratingInfoService.item(id));
    }

}
